package com.example.fileupload.controller;

import com.example.fileupload.dto.ApiResponse;
import com.example.fileupload.dto.UploadCheckRequest;
import com.example.fileupload.dto.UploadCheckResponse;
import com.example.fileupload.dto.BatchCheckRequest;
import com.example.fileupload.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

@RestController
@RequestMapping("/upload")
public class FileUploadController {

    @Autowired
    private FileUploadService fileUploadService;

    /**
     * 检查文件是否存在（秒传检查）
     */
    @PostMapping("/check")
    public ApiResponse<UploadCheckResponse> checkFile(@RequestBody UploadCheckRequest request) {
        try {
            UploadCheckResponse response = fileUploadService.checkFile(request);
            return ApiResponse.success(response);
        } catch (Exception e) {
            return ApiResponse.error("检查文件失败: " + e.getMessage());
        }
    }

    /**
     * 上传文件分片
     */
    @PostMapping("/chunk")
    public ApiResponse<String> uploadChunk(
            @RequestParam("md5") String md5,
            @RequestParam("chunkIndex") int chunkIndex,
            @RequestParam("totalChunks") int totalChunks,
            @RequestParam("file") MultipartFile chunk) {
        try {
            boolean success = fileUploadService.uploadChunk(md5, chunkIndex, totalChunks, chunk);
            if (success) {
                return ApiResponse.success("分片上传成功");
            } else {
                return ApiResponse.error("分片上传失败");
            }
        } catch (Exception e) {
            return ApiResponse.error("分片上传失败: " + e.getMessage());
        }
    }

    /**
     * 获取已上传的分片列表
     */
    @GetMapping("/chunks/{md5}")
    public ApiResponse<List<Integer>> getUploadedChunks(@PathVariable String md5) {
        try {
            List<Integer> uploadedChunks = fileUploadService.getUploadedChunks(md5);
            return ApiResponse.success(uploadedChunks);
        } catch (Exception e) {
            return ApiResponse.error("获取已上传分片失败: " + e.getMessage());
        }
    }

    /**
     * 合并文件分片
     */
    @PostMapping("/merge")
    public ApiResponse<String> mergeChunks(
            @RequestParam("md5") String md5,
            @RequestParam("fileName") String fileName,
            @RequestParam("totalChunks") int totalChunks,
            @RequestParam("fileSize") long fileSize) {
        try {
            boolean success = fileUploadService.mergeChunks(md5, fileName, totalChunks, fileSize);
            if (success) {
                return ApiResponse.success("文件上传成功");
            } else {
                return ApiResponse.error("文件合并失败");
            }
        } catch (Exception e) {
            return ApiResponse.error("文件合并失败: " + e.getMessage());
        }
    }

    /**
     * 批量检查文件是否存在（多文件秒传检查）
     */
    @PostMapping("/batch-check")
    public ApiResponse<Map<String, UploadCheckResponse>> batchCheckFiles(@RequestBody BatchCheckRequest request) {
        try {
            Map<String, UploadCheckResponse> results = new HashMap<>();
            
            for (BatchCheckRequest.FileCheckItem fileItem : request.getFiles()) {
                UploadCheckRequest checkRequest = new UploadCheckRequest(
                    fileItem.getMd5(), 
                    fileItem.getFileName(), 
                    fileItem.getFileSize()
                );
                UploadCheckResponse response = fileUploadService.checkFile(checkRequest);
                results.put(fileItem.getMd5(), response);
            }
            
            return ApiResponse.success(results);
        } catch (Exception e) {
            return ApiResponse.error("批量检查文件失败: " + e.getMessage());
        }
    }
} 